package com.show.web.role;

import com.show.dto.sys_menu.SysMenu;
import com.show.dto.sys_role.SysRole;
import com.show.dto.sys_role.SysRoleMenu;
import com.show.dto.sys_role.Ztree;
import com.show.filter.LoginController;
import com.show.service.role.RoleMenuService;
import com.show.service.role.RoleService;
import com.show.service.sys_menu.MenuService;
import com.show.util.HelperClazz;
import com.show.util.PageUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.List;
import java.util.Map;

/**
 * 角色数据控制器
 *
 * @author yxd
 * @Param
 * @date 2018-1-2
 */
@Controller
@RequestMapping(value = "/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleMenuService roleMenuService;
    @Autowired
    private LoginController loginController;

    @RequestMapping("/toRole")
    @ApiOperation(value = "角色管理首页", notes = "")
    public String toGrid(Map<String, Object> map, HttpServletRequest request) throws Exception {
        Map<String, Cookie> cookieMap = HelperClazz.ReadCookieMap(request);
        Cookie cookie = (Cookie) cookieMap.get("userName");
        String userName = cookie.getValue().replace("'","");
        List<SysMenu> list = loginController.getMenus(userName);
        String empName = loginController.getEmp(userName);
        map.put("list",list);
        map.put("empName",empName);
        return "role_manage/role_index";
    }

    @RequestMapping("/toRoleList")
    @ApiOperation(value = "角色管理列表", notes = "")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleName", paramType = "query", value = "角色名称", required = true, dataType = "String")
            , @ApiImplicitParam(name = "roleCode", paramType = "query", value = "角色编码", required = true, dataType = "String")
            , @ApiImplicitParam(name = "roleContent", paramType = "query", value = "角色描述", required = true, dataType = "String")
    })
    public String toGridList(Map<String, Object> map, SysRole role, Integer page, Integer pageSize) throws Exception {
        PageUtil<SysRole> data=roleService.findAllByPage(role,page,pageSize);
        if(StringUtils.isNotBlank(role.getRoleName())){
            map.put("roleName",role.getRoleName());
        }
        if(StringUtils.isNotBlank(role.getRoleCode())){
            map.put("roleCode",role.getRoleCode());
        }
        if(StringUtils.isNotBlank(role.getRoleContent())){
            map.put("roleContent",role.getRoleContent());
        }
        map.put("datas",data);
        return "role_manage/role_list";
    }

    @RequestMapping("/toRoleInsert")
    @ApiOperation(value = "角色添加页面", notes = "")
    public String toGridInsert(Map<String, Object> map) throws Exception {
        return "role_manage/role_insert";
    }

    @ApiOperation(value = "新增角色信息", notes = "角色编码、角色名称、角色描述")
    @PostMapping(value = "save")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "roleCode", paramType = "query", value = "角色编码", required = true, dataType = "String")
            , @ApiImplicitParam(name = "roleName", paramType = "query", value = "角色名称", required = true, dataType = "String")
            , @ApiImplicitParam(name = "roleContent", paramType = "query", value = "角色描述", required = true, dataType = "String")
    })
    public void save(SysRole role) {
         roleService.save(role);
    }

    @ApiOperation(value = "新增时校验角色信息", notes = "角色编码、角色名称、角色描述")
    @PostMapping(value = "checkRoleUpdate")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "roleCode", paramType = "query", value = "角色编码", required = true, dataType = "String")
            , @ApiImplicitParam(name = "roleName", paramType = "query", value = "角色名称", required = true, dataType = "String")
            , @ApiImplicitParam(name = "roleContent", paramType = "query", value = "角色描述", required = true, dataType = "String")
    })
    public String checkRoleUpdate(SysRole role) {
        return roleService.checkRoleUpdate(role);
    }
    @ApiOperation(value = "修改时校验角色信息", notes = "角色编码、角色名称、角色描述")
    @PostMapping(value = "checkRoleUpdate1")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "roleCode", paramType = "query", value = "角色编码", required = true, dataType = "String")
            , @ApiImplicitParam(name = "roleName", paramType = "query", value = "角色名称", required = true, dataType = "String")
            , @ApiImplicitParam(name = "roleContent", paramType = "query", value = "角色描述", required = true, dataType = "String")
    })
    public String checkRoleUpdate1(SysRole role) {
        return roleService.checkRoleUpdate1(role);
    }

    @RequestMapping("/update/{roleId}")
    @ApiOperation(value = "角色修改页面", notes = "角色ID")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleId", paramType = "query", value = "角色ID", dataType = "String")
    })
    public String update(Map<String, Object> map,@PathVariable("roleId")int roleId) throws Exception {
        SysRole role=roleService.findOne(roleId);
        map.put("role",role);
        return "role_manage/role_update";
    }

    @RequestMapping("/toMenu/{roleId}")
    @ApiOperation(value = "角色菜单权限", notes = "角色ID")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleId", paramType = "query", value = "角色ID", dataType = "String")
    })
    public String toMenu(Map<String, Object> map,@PathVariable("roleId")String roleId) throws Exception {
        List<Ztree> menu=menuService.tree();//所有菜单
        List<SysRoleMenu> roleMenu=roleMenuService.findAllByRoleId(roleId);//该角色对应的菜单
        SysRole role=roleService.findOne(Integer.parseInt(roleId));
        map.put("roleName",role.getRoleName());
        map.put("menu",menu);
        map.put("roleMenu",roleMenu);
        return "role_manage/role_menu";
    }

    @PostMapping("/saveMenu")
    @ResponseBody
    @ApiOperation(value = "角色菜单权限", notes = "角色ID")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleId", paramType = "query", value = "角色ID", dataType = "String")
            ,@ApiImplicitParam(name = "menuIds", paramType = "query", value = "菜单ID", dataType = "String")
    })
    public String saveMenu(Map<String, Object> map,SysRole role) throws Exception {
        String flag=roleService.insertMenu(role);
        return flag;
    }

    @ApiOperation(value = "校验角色信息", notes = "角色编码、角色名称、角色描述")
    @PostMapping(value = "updateAndSave")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "roleCode", paramType = "query", value = "角色编码", required = true, dataType = "String")
            , @ApiImplicitParam(name = "roleName", paramType = "query", value = "角色名称", required = true, dataType = "String")
            , @ApiImplicitParam(name = "roleContent", paramType = "query", value = "角色描述", required = true, dataType = "String")
    })
    public void updateAndSave(SysRole role) {
        roleService.updateAndSave(role);
    }

    @PostMapping("/deleteRole")
    @ApiOperation(value = "删除角色", notes = "角色ID")
    @ResponseBody
    @ApiImplicitParams({@ApiImplicitParam(name = "roleId", paramType = "query", value = "角色ID", dataType = "String")
    })
    public String deleteRole(Map<String, Object> map,SysRole role) throws Exception {
        return roleService.deleteRole(role.getRoleId());
    }


















    @GetMapping("/header")
    @ApiOperation(value = "跳转RoleInsert", notes = "")
    public String header(Map<String, Object> map, Principal principal) throws Exception {
//        CgEmp cgEmp = cgEmpService.getEmp(principal.getName());
//        map.put("emp", cgEmp);
        return "header";
    }

}
